package org.example.sdk;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
class ResourceEncoder {
    private static final ObjectMapper objectMapper = new ObjectMapper();

    private final TypeFactory typeFactory = TypeFactory.defaultInstance();

    public <T> ListResult<T> decodeListResult(Class<? extends T> type, JsonNode data) throws IOException {
        JavaType valueType = typeFactory.constructParametricType(ListResult.class, type);
        return objectMapper.treeToValue(data, valueType);
    }
}
